package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyDeatilCallbackBean;
import com.chuanjiabao.wisdom.databinding.ItemSuccessorListBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.util.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 传承人列表适配器
 */
public class SuccessorListAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<FamilyDeatilCallbackBean.DataBean.MembersBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;
    List<TextView> listTexts = new ArrayList<>();

    public SuccessorListAdapter(Context context, List<FamilyDeatilCallbackBean.DataBean.MembersBean> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemSuccessorListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_successor_list, parent, false);
        binding.setAdapter(this);
        listTexts.add(binding.tvModuleSelect);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;
        GlideUtil.setCircleMethod(mcontext, strings.get(position).getD_head_pic(), tv.ivModule);
        tv.tvModuleName.setText(strings.get(position).getD_name());

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemClick(holder.itemView, pos);
            });

            holder.itemView.setOnLongClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                return false;
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<FamilyDeatilCallbackBean.DataBean.MembersBean> {
        ItemSuccessorListBinding functionBinding;
        ImageView ivModule;
        TextView tvModuleName;

        public TourViewHolder(ItemSuccessorListBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            ivModule = dataBinding.ivModule;
            tvModuleName = dataBinding.tvModuleName;
        }

        @Override
        public void bindData(FamilyDeatilCallbackBean.DataBean.MembersBean data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

    public void setOnSelectClickListener(int position){
        for(TextView tv : listTexts){
            tv.setVisibility(View.GONE);
        }
        listTexts.get(position).setVisibility(View.VISIBLE);
    }
}